package com.portal.smail;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Class to send a e-mail.
 * @author xe33636
 */
public class SendMail {

	/**
	 * Properties for sending mail.
	 */
	private Properties sessionProperties;
	/**
	 * Properties for sending mail.
	 */
	private Properties mailProperties;
	/**
	 * Session for the new mail.
	 */
	private Session session;
	/**
	 * Message to send into mail.
	 */
	private String message;
	/**
	 * Default constructor.
	 * @param messageIn Message to send into mail.
	 */
	public SendMail(final String messageIn) {
		if (loadPropertie()) {
			message = messageIn;
			sendMail();
		}
	}
	/**
	 * Load a existing propertie file into the same path of directory.
	 * @return True - if the properties has been loaded.
	 * False - if the properties has not been loaded.
	 */
	private Boolean loadPropertie() {
		try {
			FileInputStream inSession = new FileInputStream("session.properties");
			FileInputStream inMail = new FileInputStream("mail.properties");
//			InputStream inSession = getClass().getResourceAsStream("mail.properties");
//			InputStream inMail = getClass().getResourceAsStream("mail.properties");
			sessionProperties = new Properties();
			mailProperties = new Properties();
			sessionProperties.load(inSession);
			mailProperties.load(inMail);
			inSession.close();
			inMail.close();
		} catch (IOException e) {
			e.printStackTrace();
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}
	/**
	 * Send a mail.
	 */
	private void sendMail() {
		MimeMessage mimeMessage = new MimeMessage(session);
		session = Session.getDefaultInstance(sessionProperties);
		session.setDebug(true);
		try {
			mimeMessage.setFrom(new InternetAddress(mailProperties.getProperty("mail.smtp.from")));
			List<String> recipientList = getRecipient(mailProperties.getProperty("mail.smtp.recipients"));
			for (String recipient : recipientList) {
				mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
			}
			mimeMessage.setSubject(mailProperties.getProperty("mail.smtp.subject"));
			mimeMessage.setText(message);
			Transport transport = session.getTransport();
			transport.connect(sessionProperties.getProperty("mail.smtp.user"),
					mailProperties.getProperty("mail.smtp.password"));
			transport.sendMessage(mimeMessage, mimeMessage.getRecipients(Message.RecipientType.TO));
//			Transport.send(mimeMessage);
			System.out.println("sent e-mail");
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * Split a string for the token ','.
	 * @param recipient String with the list of recipients.
	 * @return List of recipients.
	 */
	private List<String> getRecipient(final String recipient) {
		List<String> recipientOut = new ArrayList<String>();
		final String []recipientArray = recipient.split(",");
		for (String newRecipient : recipientArray) {
			recipientOut.add(newRecipient.trim());
		}
		return recipientOut;
	}
	/**
	 * Main.
	 * @param args Arguments.
	 */
	public static void main(final String []args) {
		new SendMail(args[0]);
	}
}
